package com.dragon.client.controller.customer;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.dragon.client.config.annotation.AddPage;
import com.dragon.client.config.annotation.Log;
import com.dragon.common.model.Insert;
import com.dragon.common.model.ResponseData;
import com.dragon.common.model.Update;
import com.dragon.logical.service.contract.ContractService;
import com.dragon.logical.service.customer.QuotesService;
import com.dragon.repository.domain.dto.QuotesDTO;
import com.dragon.repository.domain.dto.QuotesProductsDTO;
import com.dragon.repository.domain.vo.QuotesProductsVO;
import com.dragon.repository.domain.vo.QuotesVO;
import com.dragon.repository.entity.Contract;
import com.dragon.repository.entity.Quotes;
import com.dragon.repository.entity.QuotesProducts;
import io.swagger.annotations.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.text.ParseException;
import java.util.List;

/**
 * 报价管理接口
 * @author Charlotte
 */
@RestController
@Api(tags = "报价管理")
@RequestMapping("quote")
public class QuoteController {

    @Autowired
    QuotesService service;
    @Autowired
    ContractService contractService;


    @GetMapping("list")
    @ApiOperation("报价列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数", required = false, type = "string"),
            @ApiImplicitParam(name = "status", value = "审核状态", required = false, type = "int"),
            @ApiImplicitParam(name = "recodeId", value = "报价人id", required = false, type = "int")
    })
    public ResponseData<List<QuotesDTO>> quotesList(
            @RequestParam Integer page,
            @RequestParam Integer limit,
            @RequestParam(required = false) String search,
            @RequestParam(required = false) Integer status,
            @RequestParam(required = false) Integer recodeId
    ){
        Page<Quotes> list = service.quotesList(page, limit, search,status,recodeId,null);
        return ResponseData.success(QuotesDTO.convertList(list.getRecords()), list.getTotal());
    }

    @GetMapping("auditList")
    @ApiOperation("报价已审核列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页码", required = true, type = "int"),
            @ApiImplicitParam(name = "limit", value = "条数", required = true, type = "int"),
            @ApiImplicitParam(name = "search", value = "搜索参数", required = false, type = "string"),
            @ApiImplicitParam(name = "recodeId", value = "业务员id", required = false, type = "int"),
    })
    public ResponseData<List<QuotesDTO>> quotesStatusList(
            @RequestParam Integer page,
            @RequestParam Integer limit,
            @RequestParam(required = false) String search,
            @RequestParam(required = false) Integer recodeId
    ){
        Page<Quotes> list = service.quotesListByContractId(page, limit, search,1,recodeId,0);
        return ResponseData.success(QuotesDTO.convertList(list.getRecords()), list.getTotal());
    }


    @PostMapping("add")
    @ApiOperation("新增报价")
    @Log
    @AddPage
    public ResponseData addQuotes(@RequestBody @Validated(Insert.class)QuotesVO vo) throws ParseException {
        Quotes quotes = QuotesVO.convert(vo);
        quotes.setProducts(QuotesProductsVO.convertList(vo.getProducts()));
        return ResponseData.success(service.addQuotes(quotes));
    }

    @PostMapping("update")
    @ApiOperation("修改报价")
    @Log
    public ResponseData updateQuotes(@RequestBody @Validated(Update.class) QuotesVO vo){
        Quotes quotes = QuotesVO.convert(vo);
        quotes.setProducts(QuotesProductsVO.convertList(vo.getProducts()));
        service.updateQuotes(quotes);
        return ResponseData.success();
    }

    @PostMapping("delete")
    @ApiOperation("删除报价")
    @Log
    public ResponseData deleteQuotes( @ApiParam(name = "id", value = "报价id", required = true, type = "int") @RequestParam Integer id){
        service.deleteQuotes(id);
        return ResponseData.success();
    }

    @PostMapping("listById")
    @ApiOperation("查看报价详情")
    public ResponseData selectById(Integer id){
        return ResponseData.success(service.selectDetailById(id));
    }

    @ApiOperation(value = "导出报价数据")
    @GetMapping(value = "export")
    public void exportEmployee(HttpServletResponse response) {
        service.exportExcel(response);
    }

    @GetMapping("informationOfQuotes")
    @ApiOperation(value = "合同总信息之报价")
    public ResponseData<QuotesDTO> informationOfQuotes(
            @ApiParam(name = "contractId", value = "合同id", required = true, type = "int")
            @RequestParam Integer contractId){
        Contract contract = contractService.selectByContractId(contractId);
        Quotes quotes = service.selectDetailById(contract.getQuotesId());
        return ResponseData.success(QuotesDTO.convert(quotes));
    }

}
